/*
    @Author: feilong
    @Time: 2019-10
    
    @Description: 增加与修改用户
*/

class User {
    constructor(form, layer){
        this.form = form
        this.layer = layer
        this.name = ""
        this.password = ""
    }

    init(){
        
    }

    //修改信息请求
    changeUserReq(data){
        //弹出loading
        this.tip = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var data = {
            user_name : $(".user_name").val(),  //登录名
            password : $(".password").val(),  //
            gold : $(".gold").val(),  //
            diamond : $(".diamond").val(),  //            
        }
        var self = this
        $.post("./change_user", data, function(res, status){
            // console.log("________success_________",res, status)
            self.changeUserRes(res, status)
        }, "json")        
    }

    changeUserRes(res, status){
        top.layer.close(this.tip)        
        if(res.error_code > 0 ){            
            top.layer.msg(res.error_msg)
            return 
        }        
        top.layer.msg("修改成功！")
        layer.closeAll("iframe")
        parent.location.reload()
    }    

    addUserReq(){
        //弹出loading
        this.tip = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var data = {
            user_name : $(".user_name").val(),  //登录名
            password : $(".password").val(),  //
            gold : $(".gold").val(),  //
            diamond : $(".diamond").val(),  //            
        }
        var self = this
        $.post("./add_user", data, function(res, status){
            // console.log("________success_________",res, status)
            self.addUserRes(res, status)
        }, "json")  

    }

    addUserRes(res, status){
        top.layer.close(this.tip)        
        if(res.error_code > 0 ){            
            top.layer.msg(res.error_msg)
            return 
        }        
        top.layer.msg("用户添加成功！")
        layer.closeAll("iframe")
        parent.location.reload()
    }
}

layui.use(['form','layer'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;


    let user = new User(form, layer)
    user.init()

    form.on("submit(add_user)",function(data){
        user.addUserReq(data)
        return false;
    });

    form.on("submit(change_user)",function(data){
        console.log("________change_user_________", data.field)
        user.changeUserReq(data)
        return false;
    });


    // //格式化时间
    // function filterTime(val){
    //     if(val < 10){
    //         return "0" + val;
    //     }else{
    //         return val;
    //     }
    // }
    // //定时发布
    // var time = new Date();
    // var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})